package offer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author cyt
 * @create 2020-05-20 13:55
 */
public class Test {

    public static void main(String[] args) throws IOException, InterruptedException {
        // 调用 url
        ArrayList<Integer> list = getDate();
        for (Integer integer : list) {
            System.out.println(integer);
        }

        // 分析数据
        histogram(list);
    }

    static ArrayList<Integer> getDate() throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec("cmd.exe /c ping -n 6 www.baidu.com");
        int status = process.waitFor();
        System.out.println(status);
        Charset gbk = Charset.forName("GBK");
        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
        String line = br.readLine();

        ArrayList<String> list = new ArrayList<>();


        ArrayList<String> panelString = new ArrayList<>();

        while (line != null) {
            panelString.add(line);
            String[] s = line.split(" ");
            for (String s1 : s) {
                if (s1.contains("ms")) {
                    list.add(s1);
                }
            }
            line = br.readLine();
        }

        for (String s : panelString) {
            System.out.println(s);
        }
/*        时间=20ms
                时间=23ms
                时间=20ms
  */
        List<String> subList = list.subList(0, list.size() - 3);
        ArrayList<Integer> ans = new ArrayList<>();
        for (String s : subList) {
            String[] split = s.split("=");
            ans.add(Integer.parseInt(split[1].replaceAll("ms", "")));
        }
        // ans.forEach(System.out::print);
        return ans;
    }

    static void histogram(ArrayList<Integer> list) {


        Integer max = Collections.max(list);
        Integer min = Collections.min(list);
        int size = list.size();
        int[] ans = new int[100];


        for (Integer integer : list) {
            ans[integer]++;
        }
        Arrays.sort(ans);

        System.out.println("最小值：" + min);

        System.out.println("最大值：" + max);

        int bins = ans[99];
        System.out.println("bins ：" + ans[99]);

        int binSize = (max - min) / ans[99];
        System.out.println("binSize ：" + (max - min) / ans[99]);


        System.out.println("histogram");

        for (int i = 0; i < bins; i++) {
            if (i == bins - 1) {
                int m = min;

                System.out.println(m + "<=RTT<=" + max + "  " + getNum2(m, max, list));

                System.out.println();

            } else {
                int a = min;

                int b = ++min;

                System.out.print(a + "<=RTT<" + b + "  " + getNum1(a, b, list));

                System.out.println();
            }
        }

    }

    static String getNum1(int min, int max, ArrayList<Integer> list) {
        int num = 0;
        for (Integer integer : list) {
            if (integer >= min && integer < max) {
                num++;
            }
        }
        String s = "";
        for (int i = 0; i < num; i++) {
            s += "X ";
        }
        return s;
    }

    static String getNum2(int min, int max, ArrayList<Integer> list) {
        int num = 0;
        for (Integer integer : list) {
            if (integer >= min && integer <= max) {
                num++;
            }
        }

        String s = "";
        for (int i = 0; i < num; i++) {
            s += "X ";
        }
        return s;
    }
}
